package com.rzjm.api.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.rzjm.api.dao.*;
import com.rzjm.api.entity.*;
import com.rzjm.api.exception.TransactionalException;
import com.rzjm.api.service.DepartmentService;
import com.rzjm.api.service.EventsRecordService;
import com.rzjm.api.util.MathUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;
import java.util.List;


@Service
public class EventsRecordServiceImpl extends BaseServiceImpl<EventsRecordDao, EventsRecord> implements EventsRecordService {

    @Resource
    private EventsRecordDao eventsRecordDao;
    @Resource
    private EventsSectionDao eventsSectionDao;
    @Resource
    private EventsRecordImgDao eventsRecordImgDao;
    @Resource
    private EventsTypeDao eventsTypeDao;
    @Resource
    private DepartmentDao departmentDao;
    @Resource
    private DepartmentService departmentService;
    @Resource
    private SysUserDao sysUserDao;
    @Resource
    private UserDao userDao;
    @Resource
    private EventsRecordRewardDao eventsRecordRewardDao;

    @Value("${web.imgprefix}")
    private String imgPrefix;//图片地址

    @Override
    @Transactional
    public int addRecord(SysUser sysUser, int user_id, int section_id, String content) {
        int ret = 0;
        EventsRecord eventsRecord = new EventsRecord();
        try{
            EventsSection section = eventsSectionDao.selectById(section_id);
            if(section != null){
                eventsRecord.setSectionId(section.getId());
                eventsRecord.setTypeChildId(section.getTypeChildId());
            }
            eventsRecord.setCompanyId(sysUser.getShopId());
            eventsRecord.setSysUserId(sysUser.getUserId());

            //安全科部门
            eventsRecord.setSafeDepartId(0);
            Department departmentSafe = departmentService.getSafeDepart(sysUser.getShopId());
            if(departmentSafe!=null){
                //安全科部门
                eventsRecord.setSafeDepartId(departmentSafe.getId());
            }
            //事件状态 0 - 未处理  1 - 安全科已处理  2 - 部门已处理  3 - 安全科已确认
            eventsRecord.setState(0);
            eventsRecord.setContent(content);
            eventsRecord.setUserId(user_id);
            eventsRecord.setCreateTime(new Date());
            ret = eventsRecordDao.insert(eventsRecord);
            if(ret <= 0){
                throw new TransactionalException("eventsRecordDao insert error");
            }
        }catch (Exception e){
            e.printStackTrace();
            throw new TransactionalException("addRecord system error");
        }
        return  eventsRecord.getId();

    }

    @Override
    public List<EventsRecord> myList(int user_id, int pageIndex, int pageSize) {
        QueryWrapper<EventsRecord> qw = new QueryWrapper<>();
        qw.eq("user_id",user_id);
        qw.orderByDesc("create_time");
        List<EventsRecord> list = eventsRecordDao.getPageEntityList(qw,pageIndex,pageSize);
        if(CollUtil.isNotEmpty(list)){
            for (EventsRecord record : list){
                record  = getRecord(record);
            }
        }
        return list;
    }

    @Override
    public List<EventsRecord> myDepartList(SysUser sysUser, int user_id, int state, int pageIndex, int pageSize) {
        QueryWrapper<EventsRecord> qw = new QueryWrapper<>();
        Department department = departmentDao.selectById(sysUser.getDeptId());
        if(department!=null){
            if(department.getDeptType() == 1){
                if(state == 0){
                    Integer[] states = {0,2};
                    qw.in("state",Arrays.asList(states));
                }if(state  == 3){
                    qw.eq("state",3);
                }else{
                    Integer[] states = {0,2,3};
                    qw.in("state", Arrays.asList(states));
                }
                qw.eq("safe_depart_id",department.getId());
            }else{
                if(state == 0){
                    qw.eq("state",1);
                } else if(state == 3){
                    qw.eq("state",3);
                }else{
                    Integer[] states = {1,3};
                    qw.in("state",Arrays.asList(states));
                }
                qw.eq("handle_depart_id",department.getId());
            }
        }
        qw.orderByDesc("create_time");
        List<EventsRecord> list = eventsRecordDao.getPageEntityList(qw,pageIndex,pageSize);
        if(CollUtil.isNotEmpty(list)){
            for (EventsRecord record : list){
                record  = getRecord(record);
            }
        }
        return list;
    }

    @Override
    public EventsRecord getRecord(EventsRecord record){
        if(record!=null){
            QueryWrapper<EventsRecordImg> qw = new QueryWrapper<>();
            qw.eq("record_id",record.getId());
            qw.eq("img_type",0);
            List<EventsRecordImg> imgs = eventsRecordImgDao.selectList(qw);
            if(CollUtil.isNotEmpty(imgs)){
                for (EventsRecordImg img : imgs){
                    img.setImgUrl( imgPrefix + img.getImgUrl());;
                }
                record.setRecordImgs(imgs);
            }


            QueryWrapper<EventsRecordImg> qw1 = new QueryWrapper<>();
            qw1.eq("record_id",record.getId());
            qw1.eq("img_type",1);
            List<EventsRecordImg> recordImgs = eventsRecordImgDao.selectList(qw1);
            if(CollUtil.isNotEmpty(recordImgs)){
                for (EventsRecordImg img : recordImgs){
                    img.setImgUrl( imgPrefix + img.getImgUrl());;
                }
                record.setRecordHandleImgs(recordImgs);
            }


            EventsType eventsType = eventsTypeDao.selectById(record.getTypeChildId());
            if(eventsType!=null){
                record.setEventTypeTitle(eventsType.getTypeName());
            }

            EventsSection section = eventsSectionDao.selectById(record.getSectionId());
            if(section!=null){
                record.setSectionContent(section.getContent());
            }
        }
        return record;
    }

}
